package com.example.refresh;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.util.ArrayList;

import jp.co.recruit_lifestyle.android.widget.WaveSwipeRefreshLayout;

public class WaveSwipeRefresh extends AppCompatActivity {
    private ListView listView;
    private WaveSwipeRefreshLayout swipeRefreshLayout;
    private ArrayList<String> arrayList = new ArrayList<>();
    private Handler handler;
    private int MSG_REFRESH = 1002;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle("水滴界面");
        setContentView(R.layout.activity_wave_swipe_refresh);

        listView = findViewById(R.id.main_list);
        swipeRefreshLayout = findViewById(R.id.wave);
        final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(WaveSwipeRefresh.this,
                android.R.layout.simple_list_item_1,arrayList);
        listView.setAdapter(arrayAdapter);

        handler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                if (msg.what==MSG_REFRESH){
                    swipeRefreshLayout.setRefreshing(false);
                    arrayAdapter.notifyDataSetChanged();
                    return true;
                }
                return false;
            }
        });

        //刷新监听
        swipeRefreshLayout.setOnRefreshListener(new WaveSwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        for (int i=0;i<14;i++) {
                            arrayList.add("下拉刷新" + i);
                        }
                        try {
                            Thread.sleep(3000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        handler.sendEmptyMessage(MSG_REFRESH);

                    }
                }).start();
            }
        });

    }
}
